import datetime
import time

from selenium.webdriver.common.by import By

from common.Utils import BlogDriver

class BlogRegTest:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:9580/sign-up.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #元素检测
    def RegEleTest(self):
        #我已有一个用户？登录，链接是否可以点击跳转
        self.driver.find_element(By.CSS_SELECTOR, "body > div > div > div.text-center.text-muted.mt-3 > a").click()
        BlogDriver.getScreeShot()
        self.driver.back()

        #用户名&昵称&密码&确认密码是否可以输入
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123")
        BlogDriver.getScreeShot()

        #“眼睛”显示填入信息按钮是否可以点击显示信息
        self.driver.find_element(By.CSS_SELECTOR,"#password_a > svg").click()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat_a > svg").click()

        #使用条款和隐私政策是否可以勾选
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()

        #注册按钮是否可以点击
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        BlogDriver.getScreeShot()

        self.driver.refresh()

    #错误注册
    def RegFailTest(self):
        testDay = datetime.datetime.now().strftime("%H%M%S")

        #输入用户名&昵称&密码&确认密码&不勾选使用条款和隐私政策选项
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("123331"+testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("123331"+testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123331"+testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123"+testDay)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        BlogDriver.getScreeShot()
        self.driver.refresh()

        #输入密码&确认密码&勾选使用条款和隐私政策选项，不输用户名&昵称
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123331" + testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123" + testDay)
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        Username = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(2) > div").text
        assert Username == "用户名不能为空"
        Nickname = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(3) > div").text
        assert Nickname == "昵称不能为空"
        BlogDriver.getScreeShot()
        self.driver.refresh()

        #输入用户名&昵称&勾选使用条款和隐私政策选项，不输入密码&确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("123331" + testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("123331" + testDay)
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        Password = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(4) > div > div").text
        assert Password == "密码不能为空"
        passwordRepeat = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > div > div").text
        assert passwordRepeat == "请检查确认密码"
        BlogDriver.getScreeShot()
        self.driver.refresh()

        #不输入用户名&昵称&密码&确认密码&勾选使用条款和隐私政策选项
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        Username = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(2) > div").text
        assert Username == "用户名不能为空"
        Nickname = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(3) > div").text
        assert Nickname == "昵称不能为空"
        Password = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(4) > div > div").text
        assert Password == "密码不能为空"
        passwordRepeat = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > div > div").text
        assert passwordRepeat == "请检查确认密码"
        BlogDriver.getScreeShot()
        self.driver.refresh()

        #输入用户名&密码&确认密码&不勾选使用条款和隐私政策选项，不输入昵称
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("123331" + testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123331" + testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123" + testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        Nickname = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(3) > div").text
        assert Nickname == "昵称不能为空"
        BlogDriver.getScreeShot()
        self.driver.refresh()

    #正确注册
    def RegSucTest(self):
        #输入用户名&昵称&密码&确认密码&勾选使用条款和隐私政策选项
        testDay = datetime.datetime.now().strftime("%H%M%S")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("28905"+testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("test"+testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("28905"+testDay)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#index_nav_avatar")
        BlogDriver.getScreeShot()
        self.driver.back()

